<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">

<title>methods - RDoc Documentation</title>


<script src="../js/navigation.js" defer></script>
<script src="../js/search.js" defer></script>
<script src="../js/search_index.js" defer></script>
<script src="../js/searcher.js" defer></script>
<script src="../js/darkfish.js" defer></script>

<script src="../js/jquery-3.2.0.min.js"></script>

<script src="../js/vue.min.js"></script>
<script src="../js/js.cookie.min.js"></script>

<link href="../css/fonts.css" rel="stylesheet">
<link id='rdoccss' href="../css/rdoc.css" rel="stylesheet">
<link href="../css/carbon17.css" rel="stylesheet">

<script type="text/javascript">
  var rdoc_rel_prefix = "../";
  var index_rel_prefix = "../";
  var darkModeCsseHref = "../css/rdoc-dm.css"
  var defaultModeCssHref = "../css/rdoc.css"
  // var cssDarkmode = Cookies.get('darkmode');
  
  if( Cookies.get("darkmode") == "true") {
	$('#rdoccss').attr("href", darkModeCsseHref);
}

//  https://cssdeck.com/blog/simple-jquery-stylesheet-switcher/

document.write('<style type="text/css">body{display:none}</style>');

</script>


</head>
<body id="top" role="document" class="file">
  <!-- this is page.html -->
  <div id='actionbar' >
    <div class='wrapper mdiv'>
      <ul class='grids g0'></ul>
    </div> 
    <!-- VERSION HEADER for 3.3.0-preview2 NOT FOUND -->
  </div> <!-- end action bar -->


 <div class='wrapper hdiv'>

 


<nav id='vapp' role="navigation">
  <div id="project-navigation">
    <div id="home-section" role="region" title="Quick navigation" class="nav-section">
  <h2><a href="../index.html" rel="home">Home</a></h2>

  <div id="table-of-contents-navigation"  >
    <a href="../table_of_contents.html#pages">Pages</a>
    <a href="../table_of_contents.html#classes">Classes</a>
    <a href="../table_of_contents.html#methods">Methods</a>
  </div>
</div>

    <div id="search-section" role="search" class="project-section initially-hidden">
  <form action="#" method="get" accept-charset="utf-8">
    <div id="search-field-wrapper">
      <input id="search-field" role="combobox" aria-label="Search"
             aria-autocomplete="list" aria-controls="search-results"
             type="text" name="search" placeholder="Search" spellcheck="false"
             title="Type to search, Up and Down to navigate, Enter to load">
    </div>

    <ul id="search-results" aria-label="Search Results"
        aria-busy="false" aria-expanded="false"
        aria-atomic="false" class="initially-hidden"></ul>
  </form>
</div>

  </div>

  
<div class="nav-section">
  <h3>Table of Contents</h3>

  <ul class="link-list" role="directory">
    <li><a href="#label-Methods">Methods</a>
    <li><a href="#label-Method+Names">Method Names</a>
    <li><a href="#label-Return+Values">Return Values</a>
    <li><a href="#label-Scope">Scope</a>
    <li><a href="#label-Overriding">Overriding</a>
    <li><a href="#label-Arguments">Arguments</a>
    <li><a href="#label-Default+Values">Default Values</a>
    <li><a href="#label-Array+Decomposition">Array Decomposition</a>
    <li><a href="#label-Array-2FHash+Argument">Array/Hash Argument</a>
    <li><a href="#label-Keyword+Arguments">Keyword Arguments</a>
    <li><a href="#label-Block+Argument">Block Argument</a>
    <li><a href="#label-Argument+Forwarding">Argument Forwarding</a>
    <li><a href="#label-Exception+Handling">Exception Handling</a>
  </ul>
</div>

  <button id='toggleThing' @click="toggleNav()" >Show/hide navigation</button>

  <div id="project-metadata">
   <div :class="isOpen ? 'block' : 'hidden' " id='toggleMe'>
    
<div id="fileindex-section" class="nav-section">
  <h3>Pages</h3>

  <ul class="link-list">
    <li><a href="../NEWS/NEWS-1_8_7.html">NEWS-1.8.7</a>
    <li><a href="../NEWS/NEWS-1_9_1.html">NEWS-1.9.1</a>
    <li><a href="../NEWS/NEWS-1_9_2.html">NEWS-1.9.2</a>
    <li><a href="../NEWS/NEWS-1_9_3.html">NEWS-1.9.3</a>
    <li><a href="../NEWS/NEWS-2_0_0.html">NEWS-2.0.0</a>
    <li><a href="../NEWS/NEWS-2_1_0.html">NEWS-2.1.0</a>
    <li><a href="../NEWS/NEWS-2_2_0.html">NEWS-2.2.0</a>
    <li><a href="../NEWS/NEWS-2_3_0.html">NEWS-2.3.0</a>
    <li><a href="../NEWS/NEWS-2_4_0.html">NEWS-2.4.0</a>
    <li><a href="../NEWS/NEWS-2_5_0.html">NEWS-2.5.0</a>
    <li><a href="../NEWS/NEWS-2_6_0.html">NEWS-2.6.0</a>
    <li><a href="../NEWS/NEWS-2_7_0.html">NEWS-2.7.0</a>
    <li><a href="../NEWS/NEWS-3_0_0_md.html">NEWS-3.0.0</a>
    <li><a href="../NEWS/NEWS-3_1_0_md.html">NEWS-3.1.0</a>
    <li><a href="../NEWS/NEWS-3_2_0_md.html">NEWS-3.2.0</a>
    <li><a href="../bsearch_rdoc.html">bsearch</a>
    <li><a href="../bug_triaging_rdoc.html">bug_triaging</a>
    <li><a href="../case_mapping_rdoc.html">case_mapping</a>
    <li><a href="../character_selectors_rdoc.html">character_selectors</a>
    <li><a href="../command_injection_rdoc.html">command_injection</a>
    <li><a href="../contributing_md.html">contributing</a>
    <li><a href="../contributing/building_ruby_md.html">building_ruby</a>
    <li><a href="../contributing/documentation_guide_md.html">documentation_guide</a>
    <li><a href="../contributing/glossary_md.html">glossary</a>
    <li><a href="../contributing/making_changes_to_ruby_md.html">making_changes_to_ruby</a>
    <li><a href="../contributing/making_changes_to_stdlibs_md.html">making_changes_to_stdlibs</a>
    <li><a href="../contributing/reporting_issues_md.html">reporting_issues</a>
    <li><a href="../contributing/testing_ruby_md.html">testing_ruby</a>
    <li><a href="../dig_methods_rdoc.html">dig_methods</a>
    <li><a href="../distribution_md.html">distribution</a>
    <li><a href="../dtrace_probes_rdoc.html">dtrace_probes</a>
    <li><a href="../encodings_rdoc.html">encodings</a>
    <li><a href="../extension_ja_rdoc.html">extension.ja</a>
    <li><a href="../extension_rdoc.html">extension</a>
    <li><a href="../fiber_md.html">fiber</a>
    <li><a href="../format_specifications_rdoc.html">format_specifications</a>
    <li><a href="../globals_rdoc.html">globals</a>
    <li><a href="../implicit_conversion_rdoc.html">implicit_conversion</a>
    <li><a href="../keywords_rdoc.html">keywords</a>
    <li><a href="../maintainers_md.html">maintainers</a>
    <li><a href="../marshal_rdoc.html">marshal</a>
    <li><a href="../memory_view_md.html">memory_view</a>
    <li><a href="../optparse/argument_converters_rdoc.html">argument_converters</a>
    <li><a href="../optparse/creates_option_rdoc.html">creates_option</a>
    <li><a href="../optparse/option_params_rdoc.html">option_params</a>
    <li><a href="../optparse/tutorial_rdoc.html">tutorial</a>
    <li><a href="../packed_data_rdoc.html">packed_data</a>
    <li><a href="../ractor_md.html">ractor</a>
    <li><a href="../regexp_rdoc.html">regexp</a>
    <li><a href="../regexp/methods_rdoc.html">methods</a>
    <li><a href="../regexp/unicode_properties_rdoc.html">unicode_properties</a>
    <li><a href="../ruby_3_3_0_preview2/COPYING.html">COPYING</a>
    <li><a href="../ruby_3_3_0_preview2/COPYING_ja.html">COPYING.ja</a>
    <li><a href="../ruby_3_3_0_preview2/LEGAL.html">LEGAL</a>
    <li><a href="../ruby_3_3_0_preview2/NEWS_md.html">NEWS</a>
    <li><a href="../ruby_3_3_0_preview2/README_ja_md.html">README.ja</a>
    <li><a href="../ruby_3_3_0_preview2/README_md.html">README</a>
    <li><a href="../security_rdoc.html">security</a>
    <li><a href="../signals_rdoc.html">signals</a>
    <li><a href="../standard_library_rdoc.html">standard_library</a>
    <li><a href="../strftime_formatting_rdoc.html">strftime_formatting</a>
    <li><a href="../syntax_rdoc.html">syntax</a>
    <li><a href="../syntax/assignment_rdoc.html">assignment</a>
    <li><a href="../syntax/calling_methods_rdoc.html">calling_methods</a>
    <li><a href="../syntax/comments_rdoc.html">comments</a>
    <li><a href="../syntax/control_expressions_rdoc.html">control_expressions</a>
    <li><a href="../syntax/exceptions_rdoc.html">exceptions</a>
    <li><a href="../syntax/literals_rdoc.html">literals</a>
    <li><a href="../syntax/methods_rdoc.html">methods</a>
    <li><a href="../syntax/miscellaneous_rdoc.html">miscellaneous</a>
    <li><a href="../syntax/modules_and_classes_rdoc.html">modules_and_classes</a>
    <li><a href="../syntax/pattern_matching_rdoc.html">pattern_matching</a>
    <li><a href="../syntax/precedence_rdoc.html">precedence</a>
    <li><a href="../syntax/refinements_rdoc.html">refinements</a>
    <li><a href="../timezones_rdoc.html">timezones</a>
    <li><a href="../windows_md.html">windows</a>
    <li><a href="../yjit/yjit_md.html">yjit</a>
    <li><a href="../yjit/yjit_hacking_md.html">yjit_hacking</a>
  </ul>
</div>

   </div>
  </div>
</nav>


<!--  carbon ads here -->

<div id='extraz'>
  <div class='adzbox-index'  >
   
  </div>
 </div>         


<main role="main" aria-label="Page syntax/methods.rdoc">

<h1 id="label-Methods">Methods<span><a href="#label-Methods">&para;</a> <a href="#top">&uarr;</a></span></h1>

<p>Methods implement the functionality of your program.  Here is a simple method definition:</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">one_plus_one</span>
  <span class="ruby-value">1</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span>
<span class="ruby-keyword">end</span>
</pre>

<p>A method definition consists of the <code>def</code> keyword, a method name, the body of the method, <code>return</code> value and the <code>end</code> keyword.  When called the method will execute the body of the method.  This method returns <code>2</code>.</p>

<p>Since Ruby 3.0, there is also a shorthand syntax for methods consisting of exactly one expression:</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">one_plus_one</span> = <span class="ruby-value">1</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span>
</pre>

<p>This section only covers defining methods.  See also the <a href="calling_methods_rdoc.html">syntax documentation on calling methods</a>.</p>

<h2 id="label-Method+Names"><a href="../Method.html"><code>Method</code></a> Names<span><a href="#label-Method+Names">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p><a href="../Method.html"><code>Method</code></a> names may be one of the operators or must start a letter or a character with the eighth bit set. It may contain letters, numbers, an <code>_</code> (underscore or low line) or a character with the eighth bit set. The convention is to use underscores to separate words in a multiword method name:</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">method_name</span>
  <span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;use underscores to separate words&quot;</span>
<span class="ruby-keyword">end</span>
</pre>

<p>Ruby programs must be written in a US-ASCII-compatible character set such as UTF-8, ISO-8859-1 etc. In such character sets if the eighth bit is set it indicates an extended character. Ruby allows method names and other identifiers to contain such characters. Ruby programs cannot contain some characters like ASCII NUL (<code>\x00</code>).</p>

<p>The following are examples of valid Ruby methods:</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">hello</span>
  <span class="ruby-string">&quot;hello&quot;</span>
<span class="ruby-keyword">end</span>

<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">こんにちは</span>
  <span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;means hello in Japanese&quot;</span>
<span class="ruby-keyword">end</span>
</pre>

<p>Typically method names are US-ASCII compatible since the keys to type them exist on all keyboards.</p>

<p><a href="../Method.html"><code>Method</code></a> names may end with a <code>!</code> (bang or exclamation mark), a <code>?</code> (question mark), or <code>=</code> (equals sign).</p>

<p>The bang methods (<code>!</code> at the end of the method name) are called and executed just like any other method. However, by convention, a method with an exclamation point or bang is considered dangerous. In Ruby’s core library the dangerous method implies that when a method ends with a bang (<code>!</code>), it indicates that unlike its non-bang equivalent, permanently modifies its receiver. Almost always, the Ruby core library will have a non-bang counterpart (method name which does NOT end with <code>!</code>) of every bang method (method name which does end with <code>!</code>) that does not modify the receiver. This convention is typically true for the Ruby core library but may or may not hold true for other Ruby libraries.</p>

<p>Methods that end with a question mark by convention return boolean, but they may not always return just <code>true</code> or <code>false</code>.  Often, they will return an object to indicate a true value (or “truthy” value).</p>

<p>Methods that end with an equals sign indicate an assignment method.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">C</span>
  <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">attr</span>
    <span class="ruby-ivar">@attr</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">attr=</span>(<span class="ruby-identifier">val</span>)
    <span class="ruby-ivar">@attr</span> = <span class="ruby-identifier">val</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">c</span> = <span class="ruby-constant">C</span>.<span class="ruby-identifier">new</span>
<span class="ruby-identifier">c</span>.<span class="ruby-identifier">attr</span>      <span class="ruby-comment">#=&gt; nil</span>
<span class="ruby-identifier">c</span>.<span class="ruby-identifier">attr</span> = <span class="ruby-value">10</span> <span class="ruby-comment"># calls &quot;attr=(10)&quot;</span>
<span class="ruby-identifier">c</span>.<span class="ruby-identifier">attr</span>      <span class="ruby-comment">#=&gt; 10</span>
</pre>

<p>Assignment methods can not be defined using the shorthand syntax.</p>

<p>These are method names for the various Ruby operators.  Each of these operators accepts only one argument.  Following the operator is the typical use or name of the operator.  Creating an alternate meaning for the operator may lead to confusion as the user expects plus to add things, minus to subtract things, etc.  Additionally, you cannot alter the precedence of the operators.</p>
<dl class="rdoc-list note-list"><dt><code>+</code>   
<dd>
<p>add</p>
</dd><dt><code>-</code>   
<dd>
<p>subtract</p>
</dd><dt><code>*</code>   
<dd>
<p>multiply</p>
</dd><dt><code>**</code>  
<dd>
<p>power</p>
</dd><dt><code>/</code>   
<dd>
<p>divide</p>
</dd><dt><code>%</code>   
<dd>
<p>modulus division, <a href="../String.html#method-i-25"><code>String#%</code></a></p>
</dd><dt><code>&amp;</code>   
<dd>
<p>AND</p>
</dd><dt><code>^</code>   
<dd>
<p>XOR (exclusive OR)</p>
</dd><dt><code>&gt;&gt;</code>  
<dd>
<p>right-shift</p>
</dd><dt><code>&lt;&lt;</code>  
<dd>
<p>left-shift, append</p>
</dd><dt><code>==</code>  
<dd>
<p>equal</p>
</dd><dt><code>!=</code>  
<dd>
<p>not equal</p>
</dd><dt><code>===</code> 
<dd>
<p>case equality.  See <a href="../Object.html#method-i-3D-3D-3D"><code>Object#===</code></a></p>
</dd><dt><code>=~</code>  
<dd>
<p>pattern match.  (Not just for regular expressions)</p>
</dd><dt><code>!~</code>  
<dd>
<p>does not match</p>
</dd><dt><code>&lt;=&gt;</code> 
<dd>
<p>comparison aka spaceship operator.  See <a href="../Comparable.html"><code>Comparable</code></a></p>
</dd><dt><code>&lt;</code>   
<dd>
<p>less-than</p>
</dd><dt><code>&lt;=</code>  
<dd>
<p>less-than or equal</p>
</dd><dt><code>&gt;</code>   
<dd>
<p>greater-than</p>
</dd><dt><code>&gt;=</code>  
<dd>
<p>greater-than or equal</p>
</dd></dl>

<p>To define unary methods minus and plus, follow the operator with an <code>@</code> as in <code>+@</code>:</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">C</span>
  <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">-@</span>
    <span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;you inverted this object&quot;</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">obj</span> = <span class="ruby-constant">C</span>.<span class="ruby-identifier">new</span>

<span class="ruby-operator">-</span><span class="ruby-identifier">obj</span> <span class="ruby-comment"># prints &quot;you inverted this object&quot;</span>
</pre>

<p>The <code>@</code> is needed to differentiate unary minus and plus operators from binary minus and plus operators.</p>

<p>You can also follow tilde and not (<code>!</code>) unary methods with <code>@</code>, but it is not required as there are no binary tilde and not operators.</p>

<p>Unary methods accept zero arguments.</p>

<p>Additionally, methods for element reference and assignment may be defined: <code>[]</code> and <code>[]=</code> respectively. Both can take one or more arguments, and element reference can take none.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">C</span>
  <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">[]</span>(<span class="ruby-identifier">a</span>, <span class="ruby-identifier">b</span>)
    <span class="ruby-identifier">puts</span> <span class="ruby-identifier">a</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">b</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">[]=</span>(<span class="ruby-identifier">a</span>, <span class="ruby-identifier">b</span>, <span class="ruby-identifier">c</span>)
    <span class="ruby-identifier">puts</span> <span class="ruby-identifier">a</span> <span class="ruby-operator">*</span> <span class="ruby-identifier">b</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">c</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">obj</span> = <span class="ruby-constant">C</span>.<span class="ruby-identifier">new</span>

<span class="ruby-identifier">obj</span>[<span class="ruby-value">2</span>, <span class="ruby-value">3</span>]     <span class="ruby-comment"># prints &quot;5&quot;</span>
<span class="ruby-identifier">obj</span>[<span class="ruby-value">2</span>, <span class="ruby-value">3</span>] = <span class="ruby-value">4</span> <span class="ruby-comment"># prints &quot;10&quot;</span>
</pre>

<h2 id="label-Return+Values">Return Values<span><a href="#label-Return+Values">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>By default, a method returns the last expression that was evaluated in the body of the method.  In the example above, the last (and only) expression evaluated was the simple sum <code>1 + 1</code>.  The <code>return</code> keyword can be used to make it explicit that a method returns a value.</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">one_plus_one</span>
  <span class="ruby-keyword">return</span> <span class="ruby-value">1</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span>
<span class="ruby-keyword">end</span>
</pre>

<p>It can also be used to make a method return before the last expression is evaluated.</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">two_plus_two</span>
  <span class="ruby-keyword">return</span> <span class="ruby-value">2</span> <span class="ruby-operator">+</span> <span class="ruby-value">2</span>
  <span class="ruby-value">1</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span>  <span class="ruby-comment"># this expression is never evaluated</span>
<span class="ruby-keyword">end</span>
</pre>

<p>Note that for assignment methods the return value will be ignored when using the assignment syntax.  Instead, the argument will be returned:</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">a=</span>(<span class="ruby-identifier">value</span>)
  <span class="ruby-keyword">return</span> <span class="ruby-value">1</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">value</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">p</span>(<span class="ruby-keyword">self</span>.<span class="ruby-identifier">a</span> = <span class="ruby-value">5</span>) <span class="ruby-comment"># prints 5</span>
</pre>

<p>The actual return value will be returned when invoking the method directly:</p>

<pre class="ruby"><span class="ruby-identifier">p</span> <span class="ruby-identifier">send</span>(<span class="ruby-value">:a=</span>, <span class="ruby-value">5</span>) <span class="ruby-comment"># prints 6</span>
</pre>

<h2 id="label-Scope">Scope<span><a href="#label-Scope">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>The standard syntax to define a method:</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">my_method</span>
  <span class="ruby-comment"># ...</span>
<span class="ruby-keyword">end</span>
</pre>

<p>adds the method to a class.  You can define an instance method on a specific class with the <code>class</code> keyword:</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">C</span>
  <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">my_method</span>
    <span class="ruby-comment"># ...</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
</pre>

<p>A method may be defined on another object.  You may define a “class method” (a method that is defined on the class, not an instance of the class) like this:</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">C</span>
  <span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">my_method</span>
    <span class="ruby-comment"># ...</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
</pre>

<p>However, this is simply a special case of a greater syntactical power in Ruby, the ability to add methods to any object.  Classes are objects, so adding class methods is simply adding methods to the <a href="../Class.html"><code>Class</code></a> object.</p>

<p>The syntax for adding a method to an object is as follows:</p>

<pre class="ruby"><span class="ruby-identifier">greeting</span> = <span class="ruby-string">&quot;Hello&quot;</span>

<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">greeting</span>.<span class="ruby-identifier">broaden</span>
  <span class="ruby-keyword">self</span> <span class="ruby-operator">+</span> <span class="ruby-string">&quot;, world!&quot;</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">greeting</span>.<span class="ruby-identifier">broaden</span> <span class="ruby-comment"># returns &quot;Hello, world!&quot;</span>
</pre>

<p><code>self</code> is a keyword referring to the current object under consideration by the compiler, which might make the use of <code>self</code> in defining a class method above a little clearer.  Indeed, the example of adding a <code>hello</code> method to the class <code>String</code> can be rewritten thus:</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-constant">String</span>.<span class="ruby-identifier ruby-title">hello</span>
  <span class="ruby-string">&quot;Hello, world!&quot;</span>
<span class="ruby-keyword">end</span>
</pre>

<p>A method defined like this is called a “singleton method”.  <code>broaden</code> will only exist on the string instance <code>greeting</code>.  Other strings will not have <code>broaden</code>.</p>

<h2 id="label-Overriding">Overriding<span><a href="#label-Overriding">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>When Ruby encounters the <code>def</code> keyword, it doesn’t consider it an error if the method already exists: it simply redefines it.  This is called <em>overriding</em>.  Rather like extending core classes, this is a potentially dangerous ability, and should be used sparingly because it can cause unexpected results.  For example, consider this irb session:</p>

<pre>&gt;&gt; &quot;43&quot;.to_i
=&gt; 43
&gt;&gt; class String
&gt;&gt;   def to_i
&gt;&gt;     42
&gt;&gt;   end
&gt;&gt; end
=&gt; nil
&gt;&gt; &quot;43&quot;.to_i
=&gt; 42</pre>

<p>This will effectively sabotage any code which makes use of the method <code>String#to_i</code> to parse numbers from strings.</p>

<h2 id="label-Arguments">Arguments<span><a href="#label-Arguments">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>A method may accept arguments.  The argument list follows the method name:</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">add_one</span>(<span class="ruby-identifier">value</span>)
  <span class="ruby-identifier">value</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span>
<span class="ruby-keyword">end</span>
</pre>

<p>When called, the user of the <code>add_one</code> method must provide an argument.  The argument is a local variable in the method body.  The method will then add one to this argument and return the value.  If given <code>1</code> this method will return <code>2</code>.</p>

<p>The parentheses around the arguments are optional:</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">add_one</span> <span class="ruby-identifier">value</span>
  <span class="ruby-identifier">value</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span>
<span class="ruby-keyword">end</span>
</pre>

<p>The parentheses are mandatory in shorthand method definitions:</p>

<pre># OK
def add_one(value) = value + 1
# SyntaxError
def add_one value = value + 1</pre>

<p>Multiple arguments are separated by a comma:</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">add_values</span>(<span class="ruby-identifier">a</span>, <span class="ruby-identifier">b</span>)
  <span class="ruby-identifier">a</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">b</span>
<span class="ruby-keyword">end</span>
</pre>

<p>When called, the arguments must be provided in the exact order.  In other words, the arguments are positional.</p>

<h3 id="label-Default+Values">Default Values<span><a href="#label-Default+Values">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>Arguments may have default values:</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">add_values</span>(<span class="ruby-identifier">a</span>, <span class="ruby-identifier">b</span> = <span class="ruby-value">1</span>)
  <span class="ruby-identifier">a</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">b</span>
<span class="ruby-keyword">end</span>
</pre>

<p>The default value does not need to appear first, but arguments with defaults must be grouped together.  This is ok:</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">add_values</span>(<span class="ruby-identifier">a</span> = <span class="ruby-value">1</span>, <span class="ruby-identifier">b</span> = <span class="ruby-value">2</span>, <span class="ruby-identifier">c</span>)
  <span class="ruby-identifier">a</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">b</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">c</span>
<span class="ruby-keyword">end</span>
</pre>

<p>This will raise a SyntaxError:</p>

<pre>def add_values(a = 1, b, c = 1)
  a + b + c
end</pre>

<p>Default argument values can refer to arguments that have already been evaluated as local variables, and argument values are always evaluated left to right. So this is allowed:</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">add_values</span>(<span class="ruby-identifier">a</span> = <span class="ruby-value">1</span>, <span class="ruby-identifier">b</span> = <span class="ruby-identifier">a</span>)
  <span class="ruby-identifier">a</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">b</span>
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">add_values</span>
<span class="ruby-comment"># =&gt; 2</span>
</pre>

<p>But this will raise a <code>NameError</code> (unless there is a method named <code>b</code> defined):</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">add_values</span>(<span class="ruby-identifier">a</span> = <span class="ruby-identifier">b</span>, <span class="ruby-identifier">b</span> = <span class="ruby-value">1</span>)
  <span class="ruby-identifier">a</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">b</span>
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">add_values</span>
<span class="ruby-comment"># NameError (undefined local variable or method `b&#39; for main:Object)</span>
</pre>

<h3 id="label-Array+Decomposition"><a href="../Array.html"><code>Array</code></a> Decomposition<span><a href="#label-Array+Decomposition">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>You can decompose (unpack or extract values from) an <a href="../Array.html"><code>Array</code></a> using extra parentheses in the arguments:</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">my_method</span>((<span class="ruby-identifier">a</span>, <span class="ruby-identifier">b</span>))
  <span class="ruby-identifier">p</span> <span class="ruby-value">a:</span> <span class="ruby-identifier">a</span>, <span class="ruby-value">b:</span> <span class="ruby-identifier">b</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">my_method</span>([<span class="ruby-value">1</span>, <span class="ruby-value">2</span>])
</pre>

<p>This prints:</p>

<pre class="ruby">{<span class="ruby-value">:a</span><span class="ruby-operator">=&gt;</span><span class="ruby-value">1</span>, <span class="ruby-value">:b</span><span class="ruby-operator">=&gt;</span><span class="ruby-value">2</span>}
</pre>

<p>If the argument has extra elements in the <a href="../Array.html"><code>Array</code></a> they will be ignored:</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">my_method</span>((<span class="ruby-identifier">a</span>, <span class="ruby-identifier">b</span>))
  <span class="ruby-identifier">p</span> <span class="ruby-value">a:</span> <span class="ruby-identifier">a</span>, <span class="ruby-value">b:</span> <span class="ruby-identifier">b</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">my_method</span>([<span class="ruby-value">1</span>, <span class="ruby-value">2</span>, <span class="ruby-value">3</span>])
</pre>

<p>This has the same output as above.</p>

<p>You can use a <code>*</code> to collect the remaining arguments.  This splits an <a href="../Array.html"><code>Array</code></a> into a first element and the rest:</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">my_method</span>((<span class="ruby-identifier">a</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">b</span>))
  <span class="ruby-identifier">p</span> <span class="ruby-value">a:</span> <span class="ruby-identifier">a</span>, <span class="ruby-value">b:</span> <span class="ruby-identifier">b</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">my_method</span>([<span class="ruby-value">1</span>, <span class="ruby-value">2</span>, <span class="ruby-value">3</span>])
</pre>

<p>This prints:</p>

<pre class="ruby">{<span class="ruby-value">:a</span><span class="ruby-operator">=&gt;</span><span class="ruby-value">1</span>, <span class="ruby-value">:b</span><span class="ruby-operator">=&gt;</span>[<span class="ruby-value">2</span>, <span class="ruby-value">3</span>]}
</pre>

<p>The argument will be decomposed if it responds to to_ary.  You should only define to_ary if you can use your object in place of an <a href="../Array.html"><code>Array</code></a>.</p>

<p>Use of the inner parentheses only uses one of the sent arguments.  If the argument is not an <a href="../Array.html"><code>Array</code></a> it will be assigned to the first argument in the decomposition and the remaining arguments in the decomposition will be <code>nil</code>:</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">my_method</span>(<span class="ruby-identifier">a</span>, (<span class="ruby-identifier">b</span>, <span class="ruby-identifier">c</span>), <span class="ruby-identifier">d</span>)
  <span class="ruby-identifier">p</span> <span class="ruby-value">a:</span> <span class="ruby-identifier">a</span>, <span class="ruby-value">b:</span> <span class="ruby-identifier">b</span>, <span class="ruby-value">c:</span> <span class="ruby-identifier">c</span>, <span class="ruby-value">d:</span> <span class="ruby-identifier">d</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">my_method</span>(<span class="ruby-value">1</span>, <span class="ruby-value">2</span>, <span class="ruby-value">3</span>)
</pre>

<p>This prints:</p>

<pre class="ruby">{<span class="ruby-value">:a</span><span class="ruby-operator">=&gt;</span><span class="ruby-value">1</span>, <span class="ruby-value">:b</span><span class="ruby-operator">=&gt;</span><span class="ruby-value">2</span>, <span class="ruby-value">:c</span><span class="ruby-operator">=&gt;</span><span class="ruby-keyword">nil</span>, <span class="ruby-value">:d</span><span class="ruby-operator">=&gt;</span><span class="ruby-value">3</span>}
</pre>

<p>You can nest decomposition arbitrarily:</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">my_method</span>(((<span class="ruby-identifier">a</span>, <span class="ruby-identifier">b</span>), <span class="ruby-identifier">c</span>))
  <span class="ruby-comment"># ...</span>
<span class="ruby-keyword">end</span>
</pre>

<h3 id="label-Array-2FHash+Argument">Array/Hash Argument<span><a href="#label-Array-2FHash+Argument">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>Prefixing an argument with <code>*</code> causes any remaining arguments to be converted to an Array:</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">gather_arguments</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">arguments</span>)
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">arguments</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">gather_arguments</span> <span class="ruby-value">1</span>, <span class="ruby-value">2</span>, <span class="ruby-value">3</span> <span class="ruby-comment"># prints [1, 2, 3]</span>
</pre>

<p>The array argument must appear before any keyword arguments.</p>

<p>It is possible to gather arguments at the beginning or in the middle:</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">gather_arguments</span>(<span class="ruby-identifier">first_arg</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">middle_arguments</span>, <span class="ruby-identifier">last_arg</span>)
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">middle_arguments</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">gather_arguments</span> <span class="ruby-value">1</span>, <span class="ruby-value">2</span>, <span class="ruby-value">3</span>, <span class="ruby-value">4</span> <span class="ruby-comment"># prints [2, 3]</span>
</pre>

<p>The array argument will capture a <a href="../Hash.html"><code>Hash</code></a> as the last entry if keywords were provided by the caller after all positional arguments.</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">gather_arguments</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">arguments</span>)
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">arguments</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">gather_arguments</span> <span class="ruby-value">1</span>, <span class="ruby-value">a:</span> <span class="ruby-value">2</span> <span class="ruby-comment"># prints [1, {:a=&gt;2}]</span>
</pre>

<p>However, this only occurs if the method does not declare any keyword arguments.</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">gather_arguments_keyword</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">positional</span>, <span class="ruby-value">keyword:</span> <span class="ruby-keyword">nil</span>)
 <span class="ruby-identifier">p</span> <span class="ruby-value">positional:</span> <span class="ruby-identifier">positional</span>, <span class="ruby-value">keyword:</span> <span class="ruby-identifier">keyword</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">gather_arguments_keyword</span> <span class="ruby-value">1</span>, <span class="ruby-value">2</span>, <span class="ruby-value">three:</span> <span class="ruby-value">3</span>
<span class="ruby-comment">#=&gt; raises: unknown keyword: three (ArgumentError)</span>
</pre>

<p>Also, note that a bare <code>*</code> can be used to ignore arguments:</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">ignore_arguments</span>(<span class="ruby-operator">*</span>)
<span class="ruby-keyword">end</span>
</pre>

<p>You can also use a bare <code>*</code> when calling a method to pass the arguments directly to another method:</p>

<pre>def delegate_arguments(*)
  other_method(*)
end</pre>

<h3 id="label-Keyword+Arguments">Keyword Arguments<span><a href="#label-Keyword+Arguments">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>Keyword arguments are similar to positional arguments with default values:</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">add_values</span>(<span class="ruby-value">first:</span> <span class="ruby-value">1</span>, <span class="ruby-value">second:</span> <span class="ruby-value">2</span>)
  <span class="ruby-identifier">first</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">second</span>
<span class="ruby-keyword">end</span>
</pre>

<p>Arbitrary keyword arguments will be accepted with <code>**</code>:</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">gather_arguments</span>(<span class="ruby-value">first:</span> <span class="ruby-keyword">nil</span>, <span class="ruby-operator">**</span><span class="ruby-identifier">rest</span>)
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">first</span>, <span class="ruby-identifier">rest</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">gather_arguments</span> <span class="ruby-value">first:</span> <span class="ruby-value">1</span>, <span class="ruby-value">second:</span> <span class="ruby-value">2</span>, <span class="ruby-value">third:</span> <span class="ruby-value">3</span>
<span class="ruby-comment"># prints 1 then {:second=&gt;2, :third=&gt;3}</span>
</pre>

<p>When calling a method with keyword arguments the arguments may appear in any order.  If an unknown keyword argument is sent by the caller, and the method does not accept arbitrary keyword arguments, an <a href="../ArgumentError.html"><code>ArgumentError</code></a> is raised.</p>

<p>To require a specific keyword argument, do not include a default value for the keyword argument:</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">add_values</span>(<span class="ruby-value">first:</span>, <span class="ruby-value">second:</span>)
  <span class="ruby-identifier">first</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">second</span>
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">add_values</span>
<span class="ruby-comment"># ArgumentError (missing keywords: first, second)</span>
<span class="ruby-identifier">add_values</span>(<span class="ruby-value">first:</span> <span class="ruby-value">1</span>, <span class="ruby-value">second:</span> <span class="ruby-value">2</span>)
<span class="ruby-comment"># =&gt; 3</span>
</pre>

<p>When mixing keyword arguments and positional arguments, all positional arguments must appear before any keyword arguments.</p>

<p>Also, note that <code>**</code> can be used to ignore keyword arguments:</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">ignore_keywords</span>(<span class="ruby-operator">**</span>)
<span class="ruby-keyword">end</span>
</pre>

<p>You can also use <code>**</code> when calling a method to delegate keyword arguments to another method:</p>

<pre>def delegate_keywords(**)
  other_method(**)
end</pre>

<p>To mark a method as accepting keywords, but not actually accepting keywords, you can use the <code>**nil</code>:</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">no_keywords</span>(<span class="ruby-operator">**</span><span class="ruby-keyword">nil</span>)
<span class="ruby-keyword">end</span>
</pre>

<p>Calling such a method with keywords or a non-empty keyword splat will result in an <a href="../ArgumentError.html"><code>ArgumentError</code></a>. This syntax is supported so that keywords can be added to the method later without affected backwards compatibility.</p>

<p>If a method definition does not accept any keywords, and the <code>**nil</code> syntax is not used, any keywords provided when calling the method will be converted to a <a href="../Hash.html"><code>Hash</code></a> positional argument:</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">meth</span>(<span class="ruby-identifier">arg</span>)
  <span class="ruby-identifier">arg</span>
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">meth</span>(<span class="ruby-value">a:</span> <span class="ruby-value">1</span>)
<span class="ruby-comment"># =&gt; {:a=&gt;1}</span>
</pre>

<h3 id="label-Block+Argument">Block Argument<span><a href="#label-Block+Argument">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>The block argument is indicated by <code>&amp;</code> and must come last:</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">my_method</span>(<span class="ruby-operator">&amp;</span><span class="ruby-identifier">my_block</span>)
  <span class="ruby-identifier">my_block</span>.<span class="ruby-identifier">call</span>(<span class="ruby-keyword">self</span>)
<span class="ruby-keyword">end</span>
</pre>

<p>Most frequently the block argument is used to pass a block to another method:</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">each_item</span>(<span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
  <span class="ruby-ivar">@items</span>.<span class="ruby-identifier">each</span>(<span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
<span class="ruby-keyword">end</span>
</pre>

<p>You are not required to give a name to the block if you will just be passing it to another method:</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">each_item</span>(<span class="ruby-operator">&amp;</span>)
  <span class="ruby-ivar">@items</span>.<span class="ruby-identifier">each</span>(<span class="ruby-operator">&amp;</span>)
<span class="ruby-keyword">end</span>
</pre>

<p>If you are only going to call the block and will not otherwise manipulate it or send it to another method, using <code>yield</code> without an explicit block parameter is preferred.  This method is equivalent to the first method in this section:</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">my_method</span>
  <span class="ruby-keyword">yield</span> <span class="ruby-keyword">self</span>
<span class="ruby-keyword">end</span>
</pre>

<h3 id="label-Argument+Forwarding">Argument Forwarding<span><a href="#label-Argument+Forwarding">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>Since Ruby 2.7, an all-arguments forwarding syntax is available:</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">concrete_method</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">positional_args</span>, <span class="ruby-operator">**</span><span class="ruby-identifier">keyword_args</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
  [<span class="ruby-identifier">positional_args</span>, <span class="ruby-identifier">keyword_args</span>, <span class="ruby-identifier">block</span>]
<span class="ruby-keyword">end</span>

<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">forwarding_method</span>(<span class="ruby-operator">...</span>)
  <span class="ruby-identifier">concrete_method</span>(<span class="ruby-operator">...</span>)
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">forwarding_method</span>(<span class="ruby-value">1</span>, <span class="ruby-value">b:</span> <span class="ruby-value">2</span>) { <span class="ruby-identifier">puts</span> <span class="ruby-value">3</span> }
<span class="ruby-comment">#=&gt;  [[1], {:b=&gt;2}, #&lt;Proc:...skip...&gt;]</span>
</pre>

<p>Calling with forwarding <code>...</code> is available only in methods defined with <code>...</code>.</p>

<pre>def regular_method(arg, **kwarg)
  concrete_method(...) # Syntax error
end</pre>

<p>Since Ruby 3.0, there can be leading arguments before <code>...</code> both in definitions and in invocations (but in definitions they can be only positional arguments without default values).</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">request</span>(<span class="ruby-identifier">method</span>, <span class="ruby-identifier">path</span>, <span class="ruby-operator">**</span><span class="ruby-identifier">headers</span>)
  <span class="ruby-identifier">puts</span> <span class="ruby-node">&quot;#{method.upcase} #{path} #{headers}&quot;</span>
<span class="ruby-keyword">end</span>

<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">get</span>(<span class="ruby-operator">...</span>)
  <span class="ruby-identifier">request</span>(<span class="ruby-value">:GET</span>, <span class="ruby-operator">...</span>) <span class="ruby-comment"># leading argument in invoking</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">get</span>(<span class="ruby-string">&#39;http://ruby-lang.org&#39;</span>, <span class="ruby-string">&#39;Accept&#39;</span> <span class="ruby-operator">=&gt;</span> <span class="ruby-string">&#39;text/html&#39;</span>)
<span class="ruby-comment"># Prints: GET http://ruby-lang.org {&quot;Accept&quot;=&gt;&quot;text/html&quot;}</span>

<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">logged_get</span>(<span class="ruby-identifier">msg</span>, <span class="ruby-operator">...</span>) <span class="ruby-comment"># leading argument in definition</span>
  <span class="ruby-identifier">puts</span> <span class="ruby-node">&quot;Invoking #get: #{msg}&quot;</span>
  <span class="ruby-identifier">get</span>(<span class="ruby-operator">...</span>)
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">logged_get</span>(<span class="ruby-string">&#39;Ruby site&#39;</span>, <span class="ruby-string">&#39;http://ruby-lang.org&#39;</span>)
<span class="ruby-comment"># Prints:</span>
<span class="ruby-comment">#   Invoking #get: Ruby site</span>
<span class="ruby-comment">#   GET http://ruby-lang.org {}</span>
</pre>

<p>Note that omitting parentheses in forwarding calls may lead to unexpected results:</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">log</span>(<span class="ruby-operator">...</span>)
  <span class="ruby-identifier">puts</span> <span class="ruby-operator">...</span>  <span class="ruby-comment"># This would be treated as `puts()...&#39;,</span>
            <span class="ruby-comment"># i.e. endless range from puts result</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">log</span>(<span class="ruby-string">&quot;test&quot;</span>)
<span class="ruby-comment"># Prints: warning: ... at EOL, should be parenthesized?</span>
<span class="ruby-comment"># ...and then empty line</span>
</pre>

<h2 id="label-Exception+Handling"><a href="../Exception.html"><code>Exception</code></a> Handling<span><a href="#label-Exception+Handling">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>Methods have an implied exception handling block so you do not need to use <code>begin</code> or <code>end</code> to handle exceptions.  This:</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">my_method</span>
  <span class="ruby-keyword">begin</span>
    <span class="ruby-comment"># code that may raise an exception</span>
  <span class="ruby-keyword">rescue</span>
    <span class="ruby-comment"># handle exception</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
</pre>

<p>May be written as:</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">my_method</span>
  <span class="ruby-comment"># code that may raise an exception</span>
<span class="ruby-keyword">rescue</span>
  <span class="ruby-comment"># handle exception</span>
<span class="ruby-keyword">end</span>
</pre>

<p>Similarly, if you wish to always run code even if an exception is raised, you can use <code>ensure</code> without <code>begin</code> and <code>end</code>:</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">my_method</span>
  <span class="ruby-comment"># code that may raise an exception</span>
<span class="ruby-keyword">ensure</span>
  <span class="ruby-comment"># code that runs even if previous code raised an exception</span>
<span class="ruby-keyword">end</span>
</pre>

<p>You can also combine <code>rescue</code> with <code>ensure</code> and/or <code>else</code>, without <code>begin</code> and <code>end</code>:</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">my_method</span>
  <span class="ruby-comment"># code that may raise an exception</span>
<span class="ruby-keyword">rescue</span>
  <span class="ruby-comment"># handle exception</span>
<span class="ruby-keyword">else</span>
  <span class="ruby-comment"># only run if no exception raised above</span>
<span class="ruby-keyword">ensure</span>
  <span class="ruby-comment"># code that runs even if previous code raised an exception</span>
<span class="ruby-keyword">end</span>
</pre>

<p>If you wish to rescue an exception for only part of your method, use <code>begin</code> and <code>end</code>.  For more details see the page on <a href="exceptions_rdoc.html">exception handling</a>.</p>

</main>

</div>  <!--  class='wrapper hdiv' -->


<footer id="validator-badges" role="contentinfo">
<p><a href="https://validator.w3.org/check/referer">Validate</a></p>
<p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.4.0.</p>
<p>Based on <a href="https://github.com/ged/darkfish/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.</p>

  
    <p><p><a href="https://ruby-doc.org">Ruby-doc.org</a> is a service of <a href="https://jamesbritt.com">James Britt</a> and <a href="https://neurogami.com">Neurogami</a>, purveyors of fine <a href='https://jamesbritt.bandcamp.com/'>dance noise</a></p>
</p>
  
  </footer>

<script type="text/javascript">


  let ads  = $("#carbonads-container").children().detach();


  function swapMode() {
    var cookieName = 'darkmode';
    var cssDarkmode = Cookies.get(cookieName);
    console.log("***** swapMode! " + cssDarkmode + " *****");


    if (cssDarkmode == "true") {
      console.log("We have dark mode, set the css to light ...");
      $('#rdoccss').attr("href", defaultModeCssHref);
      $('#cssSelect').text("Dark mode");
      cssDarkmode = "false";
      console.log("swapMode! Now set cookie to " + cssDarkmode);
      Cookies.set(cookieName, cssDarkmode);

    } else {
      console.log("We not have dark mode, set the css to dark ...");
      $('#rdoccss').attr("href", darkModeCsseHref);
      $('#cssSelect').text("Light mode");
      cssDarkmode = "true";
      console.log("swapMode! Now set cookie to " + cssDarkmode);
      Cookies.set(cookieName, cssDarkmode);

    }

    console.log("  --------------- ");
  }


const vueCssApp = new Vue({
el: '#menubar',
data: {
isDark: false
},
methods: {
toggleClass: function(event){
this.isDark = !this.isDark;
}
}
})

const vueApp = new Vue({
el: '#vapp',
data: { 
isOpen: true
},

mounted() {
this.handleResize();
this.manage_mob_classes();
window.addEventListener('resize', this.handleResize)
//this.isOpen !=  (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
},
destroyed() {
window.removeEventListener('resize', this.handleResize)
},
created() {
//manage_mob_classes();
},

methods : {
isMobile() {
  return (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
},

  handleResize() {
    if (!this.isMobile()) {
      this.isOpen = window.innerWidth > 800;
    }
  },

  manage_mob_classes() {
    if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
      $("nav").addClass("mob_nav");
      $("main").addClass("mob_main");
      $("#extraz").addClass("mob_extraz");
      $("#carbonads-container").addClass("mob_carbonads-container");
      this.isOpen  = false;
    } else {
      $("nav").removeClass("mob_nav") 
        $("main").removeClass("mob_main");
      $("#extraz").removeClass("mob_extraz");
      $("#carbonads-container").removeClass("mob_carbonads-container");
      this.isOpen  = true;
    }
  },

  toggleNav() {
    this.isOpen =! this.isOpen ;
    // alert("Toggle nav!");
    console.log("toggleNav() click: " + this.isOpen );
  }
}
})

$("#carbonads-container").append(ads);


$(function() {

    var darkmode = Cookies.get("darkmode");
    console.log("Document ready: " + darkmode);

    if ( darkmode  == "true" ) {
      $('#cssSelect').text("Light mode");
    } else {
      $('#cssSelect').text("Dark mode");
     }

    $('body').css('display','block');
    });

</script>

    
  </body> 
</html>

